/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp.message;

import java.util.List;

import javax.jms.JMSException;

import com.sinodata.bsm.common.message.AsyncSession;
import com.sinodata.bsm.common.message.ServiceMessage;
import com.sinodata.bsm.common.message.SyncSession;
import com.sinodata.bsm.common.vo.Event;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-7 下午4:57:41          tangli_ITSM        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public final class Messenger {
    private static Messenger instance = new Messenger();

    private static final String evtQueueName = "ASY_EVT";
    private String syncQueueName = null;
    private SyncSession syncSession = null;
    private AsyncSession asynSession = null;

    private AsyncSession evtAsyncSession = null;

    /**
     * 
     */
    private Messenger() {
        init(1L);
    }

    public static Messenger getInstance() {
        return instance;
    }

    public synchronized void init(Long probeId) {
        this.syncQueueName = "SYNC_PTC";
        this.syncSession = new SyncSession(syncQueueName);
        this.asynSession = new AsyncSession("PROP_" + probeId);
        this.evtAsyncSession = new AsyncSession(evtQueueName); //异步发送事件
    }

    public Object sendServiceMessage(ServiceMessage msg) throws JMSException {
        return syncSession.send(msg);
    }

    public void sendPropertyValue(ResPropertyValue value) throws JMSException {
        this.asynSession.send(value);
    }

    public void sendPropertyValue(List<ResPropertyValue> values) throws JMSException {
        if (values != null && values.size() > 0) {
            for (ResPropertyValue value : values) {
                sendPropertyValue(value);
            }
        }
    }

    /**
     * 异步发送事件
     * @param event
     * @throws JMSException
     */
    public void sendEvent(Event event) throws JMSException {
        this.evtAsyncSession.send(event);
    }

    /**
     * 异步发送事件
     * @param events
     * @throws JMSException
     */
    public void sendEvent(List<Event> events) throws JMSException {
        if (events != null && events.size() > 0) {
            for (int i = 0; i < events.size(); i++) {
                sendEvent(events.get(i));
            }
        }
    }
}
